package com.lintf.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.bot.domain.BotDailyLike;
import com.lintf.bot.domain.constant.Constant;
import com.lintf.bot.domain.dto.BotDailyLikeAddDTO;
import com.lintf.bot.domain.dto.BotDailyLikeListDTO;
import com.lintf.bot.domain.enums.NoticeTypeEnums;
import com.lintf.bot.domain.enums.StatusEnums;
import com.lintf.bot.domain.vo.BotDailyLikeListVo;
import com.lintf.bot.mapper.BotDailyLikeMapper;
import com.lintf.bot.notice.NoticeUtils;
import com.lintf.bot.service.IBotDailyLikeService;
import com.lintf.common.core.domain.entity.SysUser;
import com.lintf.common.utils.SecurityUtils;
import com.lintf.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lintf
 * @description 针对表【bot_daily_like】的数据库操作Service实现
 * @createDate 2024-01-18 17:06:26
 */
@Service
public class BotDailyLikeServiceImpl extends ServiceImpl<BotDailyLikeMapper, BotDailyLike>
        implements IBotDailyLikeService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private NoticeUtils noticeUtils;

    @Override
    @Transactional
    public void add(BotDailyLikeAddDTO dto) {
        Long userId = SecurityUtils.getUserId();
        String currentNickName = SecurityUtils.getLoginUser().getUser().getNickName();
        String noticeTitle = currentNickName + Constant.LIKE;
        LambdaQueryWrapper<BotDailyLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BotDailyLike::getRefDailyId, dto.getRefDailyId());
        wrapper.eq(BotDailyLike::getUserId, userId);
        BotDailyLike selectOne = baseMapper.selectOne(wrapper);
        if (selectOne != null) {
            if (selectOne.getStatus() == StatusEnums.COMPLETED.getValue()) {
                selectOne.setStatus(StatusEnums.INCOMPLETE.getValue());
            } else {
                selectOne.setStatus(StatusEnums.COMPLETED.getValue());
                noticeUtils.personPush(dto.getRefDailyId(), noticeTitle, NoticeTypeEnums.DAILY_LIKE, noticeTitle, dto.getPublishUserId(), dto.getGroupId());
            }
//            selectOne.setUpdateParams();
            baseMapper.updateById(selectOne);
        } else {
            BotDailyLike botDailyLike = new BotDailyLike();
            BeanUtils.copyProperties(dto, botDailyLike);
            botDailyLike.setStatus(StatusEnums.COMPLETED.getValue());
            botDailyLike.setUserId(userId);
//            botDailyLike.setCreateParams();
            baseMapper.insert(botDailyLike);
            noticeUtils.personPush(dto.getRefDailyId(), noticeTitle, NoticeTypeEnums.DAILY_LIKE, noticeTitle, dto.getPublishUserId(), dto.getGroupId());

        }

    }

    @Override
    public List<BotDailyLikeListVo> selectDailyLikeList(BotDailyLikeListDTO dto) {
        LambdaQueryWrapper<BotDailyLike> likeWrapper = new LambdaQueryWrapper<>();
        if (dto.getId() != null) {
            likeWrapper.eq(BotDailyLike::getId, dto.getId());
        }
        likeWrapper.eq(BotDailyLike::getRefDailyId, dto.getRefDailyId());
        likeWrapper.eq(BotDailyLike::getStatus, StatusEnums.COMPLETED.getValue());
        likeWrapper.orderByAsc(BotDailyLike::getUpdateTime);
        List<BotDailyLike> dailyLikes = baseMapper.selectList(likeWrapper);
        List<BotDailyLikeListVo> result = new ArrayList<>();
        BotDailyLikeListVo vo;
        for (BotDailyLike botDailyLike : dailyLikes) {
            vo = new BotDailyLikeListVo();
            BeanUtils.copyProperties(botDailyLike, vo);
            SysUser sysUser = sysUserService.selectUserById(botDailyLike.getUserId());
            vo.setNickName(sysUser.getNickName());
            result.add(vo);
        }
        return result;
    }
}




